package com.atguigu.gmall.order.config;

import com.atguigu.gmall.common.config.MybatisPlusConfig;
import com.atguigu.gmall.feign.AppFeignConfig;
import com.atguigu.gmall.mq.AppRabbitConfig;
import com.atguigu.gmall.mq.RabbitConst;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@EnableFeignClients(basePackages = {
        "com.atguigu.gmall.feign.ware",
        "com.atguigu.gmall.feign.product",
        "com.atguigu.gmall.feign.cart",
        "com.atguigu.gmall.feign.pay"
        })
@Configuration
@Import({AppFeignConfig.class,
        MybatisPlusConfig.class,
        AppRabbitConfig.class})
public class AppOrderConfig {

        //配置自己的业务线程池 核心业务线程池
        @Bean("corePool") //executor 执行人
        public ThreadPoolExecutor executor(ThreadConfigProperties properties) {
                return new ThreadPoolExecutor(properties.core,  //核心线程
                        properties.max,  //最大线程数
                        properties.keepalive, //线程池 1min了都没有活要干了  //空闲时间
                        TimeUnit.MILLISECONDS,  //空闲时间单位
                        new LinkedBlockingDeque<>(properties.queueLength), //线程队列
                        //new MyItemServiceThreadFactory(),  //线程工厂
                        Executors.defaultThreadFactory(), //默认的线程工厂
                        new ThreadPoolExecutor.AbortPolicy() //拒绝策略
                );
        }

        @Component //容器中
        @ConfigurationProperties(prefix = "service.thread") //访问配置文件
        @Data
        class ThreadConfigProperties {
                private Integer core = 8;
                private Integer max = 16;
                private Long keepalive = 1000*60L;
                private Integer queueLength = 100000;
        }

}
